﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebTest.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.13.3/math.js"></script>
</head>
<body>
    <form id="form1" runat="server">
<%--    <div>
        <label class="unoCss">Uno: </label><input id="uno" class="unoCss" /><br class="unoCss" />
        <label>Due: </label><input id="due" class="uno" /><br />
        <label>Chk: </label><input id="Text2" class="uno" type="checkbox" /><br />
        <input id="Text1" class="uno" type="button" title="Premi qui" value="Premi qui" />
    </div>--%>

    <textarea id="content" name="content" readonly="readonly"></textarea>

    <input type="button" value="Add" onclick="Add()" />
    <input type="button" value="Check" onclick="Check()" />
    </form>
</body>

<script type="text/javascript">
    var m_lastCommand = null;

    function Add() {
        tinymce.activeEditor.execCommand('mceInsertContent', false, "some text");
    }

    function Check() {
        var content = tinymce.get('content').getContent({ format: 'text' });

        var retValue = '';
        try
        {
            var node = math.parse(content);

            retValue = 'Syntax OK';
        }
        catch (ex)
        {
            retValue = ex;
        }

        alert(retValue);
    }


    tinymce.PluginManager.add('btn1', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btn1', {
            text: '1',
            icon: false,
            onclick: function () {
                // Open window
                m_lastCommand = null;
                editor.insertContent('1');
            }
        });
    });

    tinymce.PluginManager.add('btn2', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btn2', {
            text: '2',
            icon: false,
            onclick: function () {
                // Open window
                m_lastCommand = null;
                editor.insertContent('2');
            }
        });
    });
    tinymce.PluginManager.add('btn3', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btn3', {
            text: '3',
            icon: false,
            onclick: function () {
                // Open window
                m_lastCommand = null;
                editor.insertContent('3');
            }
        });
    });
    tinymce.PluginManager.add('btn4', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btn4', {
            text: '4',
            icon: false,
            onclick: function () {
                // Open window
                m_lastCommand = null;
                editor.insertContent('4');
            }
        });
    });
    tinymce.PluginManager.add('btn5', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btn5', {
            text: '5',
            icon: false,
            onclick: function () {
                // Open window
                m_lastCommand = null;
                editor.insertContent('5');
            }
        });
    });
    tinymce.PluginManager.add('btn6', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btn6', {
            text: '6',
            icon: false,
            onclick: function () {
                // Open window
                m_lastCommand = null;
                editor.insertContent('6');
            }
        });
    });
    tinymce.PluginManager.add('btn7', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btn7', {
            text: '7',
            icon: false,
            onclick: function () {
                // Open window
                m_lastCommand = null;
                editor.insertContent('7');
            }
        });
    });

    tinymce.PluginManager.add('btnPlus', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btnPlus', {
            text: '+',
            icon: false,
            onclick: function () {
                // Open window
                if(!CheckCommands('+')) return;

                editor.insertContent('+');
            }
        });
    });

    tinymce.PluginManager.add('btnMinus', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btnMinus', {
            text: '-',
            icon: false,
            onclick: function () {
                // Open window
                if (!CheckCommands('-')) return;

                editor.insertContent('-');
            }
        });
    });

    tinymce.PluginManager.add('btnPer', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btnPer', {
            text: '*',
            icon: false,
            onclick: function () {
                // Open window
                if (!CheckCommands('*')) return;

                editor.insertContent('*');
            }
        });
    });

    tinymce.PluginManager.add('btnDiv', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btnDiv', {
            text: '/',
            icon: false,
            onclick: function () {
                // Open window
                if (!CheckCommands('/')) return;

                editor.insertContent('/');
            }
        });
    });

    tinymce.PluginManager.add('btnOpenPar', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btnOpenPar', {
            text: '(',
            icon: false,
            onclick: function () {
                // Open window
                editor.insertContent('(');
            }
        });
    });

    tinymce.PluginManager.add('btnClosePar', function (editor, url) {
        // Add a button that opens a window
        editor.addButton('btnClosePar', {
            text: ')',
            icon: false,
            onclick: function () {
                // Open window
                editor.insertContent(')');
            }
        });
    });

    tinymce.init({
        selector: "textarea",
        plugins: "btn1, btn2, btn3, btn4, btn5, btn6, btn7, btnPlus, btnMinus, btnPer, btnDiv, btnOpenPar, btnClosePar",
        toolbar: "btn1 | btn2 | btn3 | btn4 | btn5 | btn6 | btn7 | btnPlus | btnMinus | btnPer | btnDiv | btnOpenPar | btnClosePar",
        readonly: 0,
        menubar: false,
        statusbar: false
        //setup: function(ed) {
        //    ed.on('PreInit', function (event) {
        //       var ed = event.target, dom = ed.dom;
        //       dom.setAttrib(ed.getBody(), 'contenteditable', 'false');
        //    })
        //}
    });

    function CheckCommands(command) {
        if (m_lastCommand != null && m_lastCommand !== undefined) {

            if ((m_lastCommand.indexOf('+') !== -1) ||
                (m_lastCommand.indexOf('-') !== -1) ||
                (m_lastCommand.indexOf('*') !== -1) ||
                (m_lastCommand.indexOf('/') !== -1)) {
                return false;
            }

        } else {
            m_lastCommand = command;
            return true;
        }
    }
</script>

</html>
